#include "director.h"

#include <string>
#include <iostream>

using namespace std;

EntryDialogDirector::EntryDialogDirector()
{
	createWidgets();

	m_entry->setText("");
	m_list->setSelectIndex(-1);
}

EntryDialogDirector::~EntryDialogDirector()
{
	destroyWidgets();
}

void EntryDialogDirector::showDialog()
{
	cout<<"EntryDialogDirector is shown."<<endl;
}

void EntryDialogDirector::showDetail()
{
	cout<<"m_entry: "<<m_entry->getText()<<endl;
	cout<<"m_list.getSelectText: "<<m_list->getSelectText()<<endl;
	cout<<"m_ok: "<<(m_ok->getEnable() ? "Enable" : "Disable")<<endl;
}

void EntryDialogDirector::changed(Widget* widget)
{
	// here is the central control of 
	// interaction between widgets
	if (widget == m_entry)
	{
		bool enable = strlen(m_entry->getText()) > 0;
		m_ok->setEnable(enable);
	}

	else if (widget == m_list)
	{
		const char *text = m_list->getSelectText();
		m_entry->setText(text);
	}

	else if (widget == m_ok)
	{
		cout<<"OK button is clicked."<<endl;
	}

	else if (widget == m_cancel)
	{
		cout<<"Cancel button is clicked."<<endl;
	}
}

void EntryDialogDirector::createWidgets()
{
	m_entry = new EntryWidget(this);
	m_list = new ListWidget(this);
	m_ok = new ButtonWidget(this);
	m_cancel = new ButtonWidget(this);
}

void EntryDialogDirector::destroyWidgets()
{
	if (m_entry)	delete m_entry;
	if (m_list)		delete m_list;
	if (m_ok)		delete m_ok;
	if (m_cancel)	delete m_cancel;
}
